package com.int_tech.timeTask194;

import com.int_tech.MessageCodeAll.dao.CodeA2Dao;
import com.int_tech.MessageCodeAll.dao.CodeA2DoubleDao;
import com.int_tech.MessageCodeAll.entity.CodeA2;
import com.int_tech.MessageCodeAll.entity.CodeA2Double;
import com.int_tech.car.dao.CarDao;
import com.int_tech.car.entity.Car;
import com.int_tech.station.dao.StationDao;
import com.int_tech.station.entity.Station;
import com.int_tech.utils.DateTool;
import com.int_tech.utils.TimePlus;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * Created by liubochao on 2018/7/12.
 */
@Component
public class CheckCodeA2ToCodeA2DoubleTimeTask {
    @Autowired
    private CodeA2Dao codeA2Dao;

    @Autowired
    private CodeA2DoubleDao codeA2DoubleDao;

    @Autowired
    private CarDao carDao;

    @Autowired
    private StationDao stationDao;


    @Scheduled(cron="0/10 * * * * ?")
    public void checkA0() throws ParseException {

        //每次遍历100条
        PageRequest pageRequest = new PageRequest(0, 10000);
//        Page<CodeA2> codeA2Page = codeA2Dao.findByCheckStatusAndCarNumberOrderByIdAsc(pageRequest,0,"010AQ7712");
//        Page<CodeA2> codeA2Page = codeA2Dao.findByCheckStatusAndStatus(pageRequest,0,1);
        Page<CodeA2> codeA2Page = codeA2Dao.findByCheckStatus(pageRequest,0);
        List<CodeA2> codeA2List = codeA2Page.getContent();



        try
        {
            //遍历待处理数据，每次需要查询前面一条
            for(CodeA2 codeA2 : codeA2List)
            {
                try {
                    codeA2.setCheckStatus(1);
                    codeA2Dao.save(codeA2);

                    /*Date date = DateTool.string2Date("2018-08-05 00:00:00","yyyy-MM-dd HH:mm:ss");
                    if(codeA2.getCreatedTime().before(date))
                    {
                        continue;
                    }*/

                    if("0.0".equals(codeA2.getLat())||"0.0".equals(codeA2.getLng()))
                    {
                        continue;
                    }

                    //校验场站内
                    if(StringUtils.isEmpty(codeA2.getLocation()))
                    {
                        continue;
                    }

                    //去查询这个车辆上一条A2数据并与之对比，如果满足条件，则保留至新表
                    //条件为，如果数据为新表内第一条，必须是开门数据，否则不添加，
                    //如果新表上面有数据，则如果动作与新表上一条重复，则不添加，否则，与老表上一条对比，如果动作不同单时间间隔小宇30秒，也不添加
                    //先查新表

                    PageRequest pageRequest1 = new PageRequest(0, 1);
                    Page<CodeA2Double> codeA2DoublePage = codeA2DoubleDao.findByCarNumberAndCreatedTimeBeforeOrderByCreatedTimeDesc(codeA2.getCarNumber(),codeA2.getCreatedTime(),pageRequest1);
                    List<CodeA2Double> codeA2DoubleList = codeA2DoublePage.getContent();

                    //如果上面没有数据，并且数据动作是开门，直接添加
                    if(codeA2DoubleList == null || codeA2DoubleList.size() == 0)
                    {
                        if(codeA2.getOpenStatus() == 0)
                        {
                            CodeA2Double codeA2Double = new CodeA2Double();

                            codeA2Double.setCarNumber(codeA2.getCarNumber());
                            codeA2Double.setCheckStatus(0);
                            codeA2Double.setCreatedTime(codeA2.getCreatedTime());
                            codeA2Double.setDataString(codeA2.getDataString());
//                            codeA2Double.setIsInStation(codeA2.getIsInStation());
                            codeA2Double.setLat(codeA2.getLat());
                            codeA2Double.setLng(codeA2.getLng());
                            codeA2Double.setLocation(codeA2.getLocation());
                            codeA2Double.setOpenStatus(codeA2.getOpenStatus());
                            codeA2Double.setStatus(0);
                            codeA2Double.setTimePlus(-0);

                            codeA2DoubleDao.save(codeA2Double);
                        }
                    }
                    else
                    {

                        //直接用数据查
                        CodeA2Double codeA2DoubleByDateString = codeA2DoubleDao.findByDataString(codeA2.getDataString());

                        if(codeA2DoubleByDateString != null)
                        {
                            continue;
                        }


                        //新表里的上一个
                        CodeA2Double lastCode2Double = codeA2DoubleList.get(0);

                        if(codeA2.getDataString().equals(lastCode2Double.getDataString()))
                        {
                            continue;
                        }
                        else
                        {
                            //还要去找旧表里的上一个

                            /*PageRequest pageRequest2 = new PageRequest(0, 1);
                            Page<CodeA2> codeA2Page1 = codeA2Dao.findByCarNumberAndCreatedTimeBeforeOrderByCreatedTimeDesc(codeA2.getCarNumber(),codeA2.getCreatedTime(),pageRequest2);
                            List<CodeA2> codeA2List1 = codeA2Page1.getContent();*/

                            //如果与新表的上一条数据动作不一致，则去对比上面的数据,
                            if(codeA2.getOpenStatus() != lastCode2Double.getOpenStatus())
                            {
                                //与旧表的上一条数据做时间对比，如果时间差大于30秒，可以添加
                                Date thisDate = codeA2.getCreatedTime();
                                Date lastDate = lastCode2Double.getCreatedTime();

                                float plus = Float.parseFloat(TimePlus.gongshiTimePlusToSecond(thisDate,lastDate,1));

                                if(codeA2.getOpenStatus() == 1)
                                {
                                    //第一条是开门记录，第二条是关门记录，时间间隔30秒以内，这一对记录都剔除
                                    if(plus < 30 && plus > -30)
                                    {
                                        //删除一对
                                        //那么上面一对是假的开关门，都要删掉
                                        codeA2DoubleDao.delete(lastCode2Double);
                                        //新的不添加
                                    }
                                    else
                                    {
                                        //添加
                                        CodeA2Double codeA2Double = new CodeA2Double();

                                        codeA2Double.setCarNumber(codeA2.getCarNumber());
                                        codeA2Double.setCheckStatus(0);
                                        codeA2Double.setCreatedTime(codeA2.getCreatedTime());
                                        codeA2Double.setDataString(codeA2.getDataString());
//                                        codeA2Double.setIsInStation(codeA2.getIsInStation());
                                        codeA2Double.setLat(codeA2.getLat());
                                        codeA2Double.setLng(codeA2.getLng());
                                        codeA2Double.setLocation(codeA2.getLocation());
                                        codeA2Double.setOpenStatus(codeA2.getOpenStatus());
                                        codeA2Double.setStatus(0);
                                        codeA2Double.setTimePlus(-0);

                                        codeA2DoubleDao.save(codeA2Double);
                                    }
                                }
                                else
                                {
                                    //如果第二条数据是开门，直接保留
                                    //添加
                                    CodeA2Double codeA2Double = new CodeA2Double();

                                    codeA2Double.setCarNumber(codeA2.getCarNumber());
                                    codeA2Double.setCheckStatus(0);
                                    codeA2Double.setCreatedTime(codeA2.getCreatedTime());
                                    codeA2Double.setDataString(codeA2.getDataString());
//                                    codeA2Double.setIsInStation(codeA2.getIsInStation());
                                    codeA2Double.setLat(codeA2.getLat());
                                    codeA2Double.setLng(codeA2.getLng());
                                    codeA2Double.setLocation(codeA2.getLocation());
                                    codeA2Double.setOpenStatus(codeA2.getOpenStatus());
                                    codeA2Double.setStatus(0);
                                    codeA2Double.setTimePlus(-0);

                                    codeA2DoubleDao.save(codeA2Double);
                                }
                            }
                           /* else
                            {
                                //如果都是开门数据，保留最新的，上一条删除
                                if(lastCode2Double.getOpenStatus() == 0)
                                {
                                    codeA2DoubleDao.delete(lastCode2Double);

                                    CodeA2Double codeA2Double = new CodeA2Double();

                                    codeA2Double.setCarNumber(codeA2.getCarNumber());
                                    codeA2Double.setCheckStatus(0);
                                    codeA2Double.setCreatedTime(codeA2.getCreatedTime());
                                    codeA2Double.setDataString(codeA2.getDataString());
                                    codeA2Double.setIsInStation(codeA2.getIsInStation());
                                    codeA2Double.setLat(codeA2.getLat());
                                    codeA2Double.setLng(codeA2.getLng());
                                    codeA2Double.setLocation(codeA2.getLocation());
                                    codeA2Double.setOpenStatus(codeA2.getOpenStatus());
                                    codeA2Double.setStatus(0);
                                    codeA2Double.setTimePlus(-0);

                                    codeA2DoubleDao.save(codeA2Double);
                                }
                            }*/
                        }
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                    codeA2.setCheckStatus(-1);
                    codeA2Dao.save(codeA2);
                    Thread.sleep(50000);
                }

            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
